

#if Windows
#	include <mutex>
#elif Android
#	include <pthread.h>
#endif

#include "CSharpGarbagePointer.h"
#include <System/NullReferenceException.h>
#include <System/Diagnostics/Debug.h>
#include <System/Type.h>

// Remove some defines so it is not collided with the std libraries.
#include <UndefCSharpKeywords.h>

void __checkNull_hasThrown()
{
	__debug_gc_ptr("thow NullReferenceException");
	throw Riccsson::System::NullReferenceException("");
}

void __debug_gc_ptr(const char* message)
{
	Riccsson::System::Diagnostics::Debug::WriteLine(message);
}

#if Windows
std::recursive_mutex gc_ptr_mutex;
#elif Android
pthread_mutex_t gc_ptr_Mutex;
pthread_mutexattr_t gc_ptr_Attr;
#endif

void _gc_ptr_lock()
{
#if Windows
	gc_ptr_mutex.lock();
#elif Android
	pthread_mutexattr_init(&gc_ptr_Attr);
	pthread_mutexattr_settype(&gc_ptr_Attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&gc_ptr_Mutex, &gc_ptr_Attr);
	
	int rc = pthread_mutex_lock(&gc_ptr_Mutex);
#endif
}

void _gc_ptr_unlock()
{
#if Windows
	gc_ptr_mutex.unlock();
#elif Android
	int rc = pthread_mutex_unlock(&gc_ptr_Mutex);
#endif
}

std::map2<void*, int> s_instancesCounter;
